package com.ict.ms.controller;

import com.ict.ms.common.StateMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.ict.ms.port.facade.api.IPortsSearchService;
import com.ict.ms.port.facade.model.PortinfoSearchLoLa_simp;
/**
 * Created by FCC on 2017/2/23.
 * 港口搜索
 */
@Controller
@RequestMapping("/portinfo")
public class PortsSearchController {
    /**
     * 港口搜索
     *
     * @Param portname 港口名字
     * @Return portNames 包含港口名字列表
     */
    @Resource
    IPortsSearchService   portsSearchService;

    @ResponseBody
    @RequestMapping(value = "portsSearch.do", method = RequestMethod.POST)
    public String portsSearch(ModelMap map, @RequestParam(value = "portname", required = false) String portname) {
        String msg;
        String jsonStr;
        String longitude;
        String latitude;
        int num = portsSearchService.getPortsInt(portname);
        if (num == 0) {
            msg = "未找到该港口名，请重新输入";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }
        //数据源
        List<PortinfoSearchLoLa_simp> portinfoSearches;
        //返回list
        List<PortinfoSearchLoLa_simp> portList = new ArrayList<PortinfoSearchLoLa_simp>();
        //查询数据库
        portinfoSearches = portsSearchService.getPortsNames(portname);
        int nuber = portinfoSearches.size();
        //处理融合数据
        if (!portinfoSearches.isEmpty()) {
            for (PortinfoSearchLoLa_simp portinfo : portinfoSearches) {
                PortinfoSearchLoLa_simp port = new PortinfoSearchLoLa_simp();
                latitude = portinfo.getLad() + "." + portinfo.getLam();
                longitude = portinfo.getLod() + "." + portinfo.getLom();
                long la = getlong(latitude, portinfo.getLah());
                long lo = getlong(longitude, portinfo.getLoh());
                port.setLo(lo);
                port.setLa(la);
                port.setCc(portinfo.getCc());
                port.setCe(portinfo.getCe());
                port.setCn(portinfo.getCn());
                port.setHs(portinfo.getHs());
                port.setHt(portinfo.getHt());
                port.setId(portinfo.getId());
                port.setPn(portinfo.getPn());
                port.setRn(portinfo.getRn());
                portList.add(port);
            }
        } else {
            msg = "未找到该港口名，请重新输入";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }
        map.addAttribute("portNames", portList);
        map.addAttribute("limit", nuber);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 固定搜索港口列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "portsSearchByLoLa.do", method = RequestMethod.GET)
    public String searchByLoLa(ModelMap map) {
        String msg;
        String jsonStr;
        String longitude;//经度
        String latitude;//伟度
        List<PortinfoSearchLoLa_simp> portList = new ArrayList<PortinfoSearchLoLa_simp>();
        List<PortinfoSearchLoLa_simp> portinfoSearchesByLaLo;
        portinfoSearchesByLaLo = portsSearchService.getPortByLoLa();
        if (!portinfoSearchesByLaLo.isEmpty()) {
            for (PortinfoSearchLoLa_simp portinfo : portinfoSearchesByLaLo) {
                PortinfoSearchLoLa_simp port = new PortinfoSearchLoLa_simp();
                latitude = portinfo.getLad() + "." + portinfo.getLam();//获取伟度
                longitude = portinfo.getLod() + "." + portinfo.getLom();//获取伟度
                long la = getlong(latitude, portinfo.getLah());
                long lo = getlong(longitude, portinfo.getLoh());
                port.setLo(lo);
                port.setLa(la);
                port.setCc(portinfo.getCc());
                port.setCe(portinfo.getCe());
                port.setCn(portinfo.getCn());
                port.setHs(portinfo.getHs());
                port.setHt(portinfo.getHt());
                port.setId(portinfo.getId());
                port.setPn(portinfo.getPn());
                port.setRn(portinfo.getRn());
                portList.add(port);
            }
        } else {
            msg = "未找到该港口";
            map.addAttribute("error", msg);
            jsonStr = StateMsg.toJson(-1, map);
            return jsonStr;
        }
        map.addAttribute("portsList", portList);
        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 返回一定经纬度区间港口信息
     *
     * @param
     * @param
     * @return 纬度区间港口
     */
    @ResponseBody
    @RequestMapping(value = "portsSearchByLoLa.do", method = RequestMethod.POST)
    public String intervalPortScarch(ModelMap map, @RequestParam(value = "laloright", required = false) String laloright,//右上经纬度
                                     @RequestParam(value = "lalolegft", required = false) String lalolegft //左下经纬度
    ) {
        String msg, jsonStr;

        return null;
    }

    //乘以经纬度
    private long getlong(String lola, String lolabq) {
        if (lolabq.equals("W") | lolabq.equals("S")) {
            Double d = Double.parseDouble(lola);
            long aoll = (long) ((d * 600000) * -1);
            return aoll;
        }
        Double d = Double.parseDouble(lola);
        long aoll = (long) (d * 600000);
        return aoll;
    }

}
